'''
Created on 28.02.2010

@author: FlashDeveloper
'''

import os
from id3batproc.ID3 import ID3
import sys


class Builder:
    
    def start(self):
        pass
    
    def build(self):
        pass

    def complete(self):
        pass
    

class UpdateArtistBuilder(Builder):
    
    ID = "upart"

    def __init__(self, artist):
        self.newArtistValue = artist
    
    def build( self, id3info ):
        id3info.artist = self.newArtistValue
        id3info.write()
        print "updArt: " + id3info.filename

    @staticmethod
    def makeInstance():
        newArtName = sys.argv[2]
        print "set art: " + newArtName
        return UpdateArtistBuilder(newArtName) 

class UpdateGenreBuilder(Builder):
    ID = "upgenre"
    def __init__(self, genreIndx):
        self.genreIndx = genreIndx
    
    def build( self, id3info):
        id3info.genre = self.genreIndx
        id3info.write()
        pass
    
    @staticmethod
    def makeInstance():
        newGenre = sys.argv[2]
        print "set art: " + newGenre
        newGenreIndx = 0
        for genre in ID3.genres:
            if genre.lower() == newGenre.lower() :
                print "genre found for " + newGenre + " indx:" , newGenreIndx
                return UpdateGenreBuilder(newGenreIndx)
            newGenreIndx += 1
        else:
            print "error cant find '" + newGenre + "' in " , ID3.genres
        
class MutantListBuilder:

    ID = "detect_mutant"
    
    def __init__(self):
        pass
    
    def start(self):
        self.artists = {}
        self.genres = {}        
    
    def build(self, id3info):
        if self.artists.has_key(id3info.artist):
            self.artists[id3info.artist] += 1
        else:
            self.artists[id3info.artist] = 1
            
        if self.genres.has_key(id3info.d["GENRE"] ):
            self.genres[id3info.d["GENRE"] ] += 1
        else:
            self.genres[id3info.d["GENRE"] ] = 1            
        
    def complete(self):
        print "artists:", self.artists
        print "genres:", self.genres   
            
    @staticmethod    
    def makeInstance():
        return MutantListBuilder()  
        
class EchoBuilder(Builder):
    
    ID = "echo"
    
    def build(self, id3info ):
        if ( id3info.title == "" ):
            print "NOT HAS TITLE: " + unicode( id3info.filename ) 
        else:
            print " title: " + id3info.title 
            
        if ( id3info.genre == "" ):
            print "genres not defined: " ,  id3info.d["GENRE"] 
        else:
            print "genres: " ,  id3info.d["GENRE"]     
            
        pass

    @staticmethod
    def makeInstance():
        return EchoBuilder()



def walk( dir , builder ):
     builder.start()
     for root, dirs, files in os.walk( dir ):
        for filename in files:
            fullFileName = os.path.join( root , filename )
            ext = os.path.splitext(fullFileName)[1]
            
            if ( ".mp3" == ext.lower() ):    
                try:
                    id3info = ID3( fullFileName )
                    builder.build( id3info )
                except:
                    print "cant process: " + fullFileName + "reason:" , sys.exc_info()   
     builder.complete()
     
     